<?php

class reserveActions extends sfActions {

    public function executeShowFancyboxContent_ajax(sfWebRequest $request){
        if(!$request->isXmlHttpRequest()){ return sfView::NONE; }
        $c = new Criteria();
        $parking_lot_id = $request->getParameter('parking_lot_id');
        $cost_per_hour_type_id = null;
        $cost_per_day_type_id = null;
        
        $c->clear();
        $c->add(ParkingLotPeer::ID, $parking_lot_id);
        $c->add(ParkingLotPeer::ENABLED, 1);
        $parking_lot = ParkingLotPeer::doSelectOne($c);
        $this->parking_lot = $parking_lot;
        
        $c->clear();
        $c->add(TypePeer::GROUP, 2);
        $c->add(TypePeer::VALUE, 1);
        $cost_per_hour_type = TypePeer::doSelectOne($c);
        if(is_object($cost_per_hour_type)){
            $cost_per_hour_type_id = $cost_per_hour_type->getId();
        }
        $this->cost_per_hour_type_id = $cost_per_hour_type_id;
        
        $c->clear();
        $c->add(TypePeer::GROUP, 2);
        $c->add(TypePeer::VALUE, 2);
        $cost_per_day_type = TypePeer::doSelectOne($c);
        if(is_object($cost_per_day_type)){
            $cost_per_day_type_id = $cost_per_day_type->getId();
        }
        $this->cost_per_day_type_id = $cost_per_day_type_id;
    }
    
    public function executeSave(sfWebRequest $request){
        $hdParkingLotId = $request->getParameter('hdParkingLotId');
        $rbReserveType = $request->getParameter('rbReserveType');
        $arrival_date = $request->getParameter('arrival_date');
        $arrival_time = $request->getParameter('arrival_time');
        $duration = $request->getParameter('duration');
        $email = $request->getParameter('email');
        $plate = $request->getParameter('plate');
        $current_user = $this->getUser()->getAttribute('current_user');
        $c = new Criteria();
                
        $conn = Propel::getConnection();
        $conn->beginTransaction();
        try {
            if($this->getUser()->isAuthenticated()){
                $user = $current_user;
            }else{
                $c->clear();
                $c->add(UserPeer::EMAIL, $email);
                $c->add(UserPeer::ENABLED, 1);
                $user = UserPeer::doSelectOne($c);
                if(!is_object($user)){
                    $user = new User();
                    $user->setEmail($email);
                    $user->setEnabled(1);
                    $user->save($conn);
                }
            }
            
            //Verifica existencia de placa para no duplicar registros
            
            $user_vehicle = new UserVehicle();
            $user_vehicle->setPlate($plate);
            $user_vehicle->setUser($user);
            $user_vehicle->save($conn);
            
            $reservation = new Reservation();
            $reservation->setParkingLotId($hdParkingLotId);
            $reservation->setReservationTypeId($rbReserveType);
            $reservation->setUserVehicle($user_vehicle);
            $reservation->setArrivalTime(/* 2014-01-01 18:00 */);
            $reservation->setDuration($duration);
            $reservation->save($conn);

            $conn->commit();
        } catch (Exception $exc) {
            error_log($exc->getTraceAsString());
            $conn->rollback();
        }
        
        return sfView::SUCCESS;
    }
    
}